<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/28
 * Time: 14:25
 */
namespace app\admin\controller;
use app\data\Data;
use app\validate\TagValidate;
use think\Config;
use think\Request;

/**
 * -------------------------------------
 * Class Tag
 * @package app\admin\controller
 * 标签管理类
 * -------------------------------------
 */
class Tag extends Check{
    /**
     * @return \think\response\View
     * 标签列表
     */
    public function tagList(){
        $tag = new Data('tag');
        $get = input('param.');
        $query = 'a.id > 0';
        if(!empty($get['search'])){
            $query .= " and tag_name like '%{$get['search']}%'";
            $this->assign('search',$get['search']);
        }
        $list = $tag->page($query,'create_time desc',Config::get('paginate.list_rows'),$query);
        return view('tagList',['list'=>$list->items(),'page'=>$list->render()]);
    }

    /**
     * @return \think\response\View
     * 添加标签
     */
    public function tagAdd(){
        try{
            if(Request::instance()->isPost()){
                $tag = new Data('tag');
                $post = Request::instance()->post();
                $rule_validate = new TagValidate();
                $rule_validate = $rule_validate->tagValidate();
                self::validateData($rule_validate['rule'],$rule_validate['message'],$post);
                $post['create_time'] = time();
                $tag_add_bool = $tag->insertData($post);
                if(!$tag_add_bool){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('tagAdd');
    }

    /**
     * @return \think\response\View
     * 更新标签
     */
    public function tagSave(){
        try{
            $tag = new Data('tag');
            if(Request::instance()->isPost()){
                $post = Request::instance()->post();
                $rule_validate = new TagValidate();
                $rule_validate = $rule_validate->tagValidate();
                self::validateData($rule_validate['rule'],$rule_validate['message'],$post);
                $tag_save_bool = $tag->updateData($post);
                if($tag_save_bool === false){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $id = intval(input("param.id"));
        $getOne = $tag->find("id = $id");
        return view('tagSave',['one'=>$getOne]);
    }

    /**
     * @return array
     * 删除数据
     */
    public function tagDelete(){
        try{
            $tag = new Data('tag');
            $id = intval(input('param.id'));
            if(!$tag->deleteByWhere("id = $id")){
                throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return self::formatSuccessResult();
    }
}